define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coursetype/coursetype/index',
                    add_url: 'coursetype/coursetype/add',
                    edit_url: 'coursetype/coursetype/edit',
                    del_url: 'coursetype/coursetype/del',
                    multi_url: 'coursetypecoursetype//multi',
                    table: 'course_type',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                escape: false, 
                search: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID', sortable: true},
                        {field: 'city_id', title: '城市'},
                        {field: 'school_id', title: '学校'},
                        {field: 'name', title: '课程类型名称', operate: 'LIKE', align: 'left'},
                        {field: 'description', title: '描述', visible: false},
                        {field: 'icon', title: '图标', formatter: Table.api.formatter.icon, align: 'left'},
                        {field: 'color', title: '颜色', formatter: function (value) { return '<span style="color:' + value + '">' + value + '</span>'; }},
                        {field: 'sort_order', title: '排序权重', sortable: true},
                        {field: 'is_active', title: '是否启用', searchList: {1: '启用', 0: '禁用'}, formatter: Table.api.formatter.status},
                        {field: 'parent_id', title: '父级ID', visible: false},
                        {field: 'level', title: '层级', visible: false},
                        {field: 'status', title: '状态', searchList: {1: '正常', 0: '禁用'}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: '权重', sortable: true},
                        {field: 'create_time', title: '创建时间', operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: '更新时间', operate: false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js';
            document.head.appendChild(script);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
            Controller.api.loadParentSelect();
        },
        edit: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
            Controller.api.loadParentSelect();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            loadParentSelect: function () {
                // 动态加载父级下拉树
                var $select = $('#c-parent_id');
                if ($select.length) {
                    $.get('coursetype/liststree', function (res) {
                        if (res.code === 1 && res.data) {
                            $select.empty();
                            $select.append('<option value="0">无</option>');
                            function renderTree(list, prefix) {
                                prefix = prefix || '';
                                list.forEach(function (item) {
                                    var label = prefix + item.name;
                                    $select.append($('<option>').val(item.id).html(label));
                                    if (item.children && item.children.length) {
                                        renderTree(item.children, prefix + '├──');
                                    }
                                });
                            }
                            renderTree(res.data, '');
                            // 编辑时选中
                            var val = $select.data('value') || $select.attr('data-value') || $select.val();
                            if (val) $select.val(val);
                        }
                    });
                }
            }
        }
    };
    return Controller;
}); 